--[[--
- @Copyright (C), 2016-, sixiong.
- @Name article.lua
- @Author sixiong
- @Version 1.0
- @Date: 2016年6月11日下午22:06:41
- @Description 文章模型层
- @Class
- @Function List
- @History <author> <time> <version > <desc>
            sixiong 2016年6月11日下午22:06:41  1.0  第一次建立该文件
--]] 
local cjson = require "cjson";
local article = model.__new( _CONF["mysql"] , "mysql");
 
--[[
- @desc    文章表 
--]]
article.table = 'mg_article';
 
--[[
- @desc   获取文章条数
- @param  int    id  字符串
- return  table
--]]
function article:count(where) 
     local str = "";
     if  where['category_id']  and #(where['category_id']) > 0 then   
	         str = str .. ' category_id IN ( ';
	          for k, v in pairs(where['category_id']) do   
	              if v then 
			        str = str .. v  .. ','; 
			      end
			  end  
			 str = string.sub(str, 1, -2);
			 str = str ..  ') ';  
	end	 
   
 	if  string.len(str) >= 3 then
		       str = " WHERE " .. str;
	end
 
    local sql = "SELECT COUNT(*) AS __count FROM `".. self.table .. "`" ..  str;  
   
    local res, err, errno, sqlstate = self.mysql(sql);   
    return res[1]['__count'];
end
 
 
--[[
- @desc   获取一条文章
- @param  int    id  字符串
- return  table
--]]
function article:getone(id)  
     if id == nil   then
        return nil; 
     end
     
     if  type(id) ~= "number" then
         id  = tonumber(id);
     end 
     
     
    local cache = model.__get("cache");   
    local res, err, errno, sqlstate = cache:get( 'artcle__result__key'.. id );  --去redis取
        
    if  type(res) == "userdata" then  -- type类型
	     local sql = 'SELECT * FROM  ' .. self.table ..' WHERE id = ' .. id; 
         res, err, errno, sqlstate = self.mysql(sql);
	     cache:set('artcle__result__key'.. id,  tostring(cjson.encode(res)));    --存redis
    else
         res = cjson.decode(res); 
    end    
     return res[1];
end
 
--[[
- @desc   获取多条文章
- @param  table where   字符串
- @param  string limit   字符串
- return  table
--]]
function article:getall(where, limit) 
     local str = "";
     if  where['category_id']  and #(where['category_id']) > 0 then   
	         str = str .. ' category_id IN ( ';
	          for k, v in pairs(where['category_id']) do   
			             str = str .. v  .. ','; 
			  end  
			 str = string.sub(str, 1, -2);
			 str = str ..  ') ';  
	end
 
 	if  string.len(str) >= 3 then
		       str = " WHERE " .. str; 
	end
  
     if limit then 
		 limit = " ORDER BY id DESC LIMIT  " .. limit;
	 end

     local sql = 'SELECT * FROM  ' .. self.table .. str .. limit;   
    -- dump(sql);
     local res, err, errno, sqlstate = self.mysql(sql); 
     return res;
end 


function article:getrecom(where, limit) 
        local str = "";
		if  where['recom']  and (where['recom']) > 0 then   
			          str = str ..  '  recom = 1 ';  
		 end
		   
	 	if  string.len(str) >= 3 then
			       str = " WHERE " .. str; 
		end
	  
	     if limit then 
			 limit = " ORDER BY id DESC LIMIT  " .. limit;
		 end
	
	     local sql = 'SELECT * FROM  ' .. self.table .. str .. limit;    
	     local res, err, errno, sqlstate = self.mysql(sql);  
	     return res;
end


--[[
- @desc   添加一条文章 
- @param  table   option 数据
- return  int   自增id
--]]
function article:add(option)
	 local res, err, errno, sqlstate = self.mysql("insert into " .. self.table .. "(name) " .. "values (343)"); 
	 return res;
end


--[[
- @desc   修改一条文章 
- @param  int     id  
- @param  table   option 数据
- return   int    影响行数
--]]
function article:update(id, option)
	 local res, err, errno, sqlstate = self.mysql("UPDATE " .. self.table .. " SET `status` = 0 WHERE `item_comment`.`id` = '67'"); 
	 return res;
end


--[[
- @desc   删除文章 
- @param  mixed  id 数据
- return   int    影响行数
--]]
function article:del(id)
	 local res, err, errno, sqlstate = self.mysql("DELETE FROM ".. self.table .. " WHERE id = 1"); 
	 return res;
end
 
 
return article;